package com.zhdl.hardware.clamping_jaw.micro.service.impl;

import com.project.common.util.ByteHelper;
import com.zhdl.hardware.clamping_jaw.micro.repository.MicroPortCmdHandleDao;
import com.zhdl.hardware.clamping_jaw.micro.service.MicroSendCmdService;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class MicroSendCmdServiceImpl implements MicroSendCmdService {

    /**
     * 夹具 夹紧/松开
     */
    public String cmd1 = ">00E1",cmd2 = ">00E2";
    /**
     * 电机 使能/解锁
     */
    public String cmd3 = ">00a1", cmd4 = ">00a0";

    @Resource
    private MicroPortCmdHandleDao microPortCmdHandleDao;

    @Override
    public void microGenericCmd(String port, int model, int instruct, CommonCallback<String> callback){
        String cmd = "";
        switch (instruct){
            case 1 ->{
                cmd = cmd1;
            }
            case 2 ->{
                cmd = cmd2;
            }
            case 3 ->{
                cmd = cmd3;
            }
            case 4 ->{
                cmd = cmd4;
            }
        }
        StringBuffer ti;
        switch (model){
            case 1 ->{
                ti = new  StringBuffer(cmd);
                ti.replace(2,3,"1");
            }
            case 2 ->{
                ti = new StringBuffer(cmd);
                ti.replace(2,3,"2");
            }
            case 3 ->{
                ti = new StringBuffer(cmd);
                ti.replace(2,3,"3");
            }
            default -> throw new IllegalStateException("Unexpected value: " + model);
        }
        log.info(ti.toString());
        microPortCmdHandleDao.microGenericCmd(port, ti.toString(), bytes -> {
            String hexResult = ByteHelper.bytesArrayToHexString(bytes);
            log.info("当前返回结果为" + hexResult);
            callback.statusCallback(hexResult);
        });
    }
}
